<?php

namespace App\Http\Controllers\Api;

use App\Http\Logic\LoginLogic;
use App\Http\Requests\Api\LoginRequest;
use Laravel\Socialite\Facades\Socialite;

class LoginController extends Controller
{

    /**
     * 手机号 ｜ 账号密码 登录
     * @param LoginRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(LoginRequest $request)
    {
        $result = LoginLogic::logic()->login($request);
        return $result ? $this->success('登录成功', $result) : $this->error(403,'登录失败');
    }

    /**
     * 第三方登录
     * @param $type
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function socialLogin($type)
    {
        return Socialite::driver($type)->redirect();
    }

    /**
     * 第三方登录回调
     * @param $type
     */
    public function socialLoginCallBack($type)
    {
        $result = LoginLogic::logic()->socialLogin($type);
        return $this->success('登录成功', $result);
    }

    public function refreshToken()
    {
        $result = LoginLogic::logic()->refreshToken();
        return $this->success('刷新成功', $result);
    }

    public function logout()
    {
        LoginLogic::logic()->logout();
        return $this->success('退出成功');
    }
}
